package org.batatasframework.support.loader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.batatasframework.properties.PropertiesResource;
import org.batatasframework.support.PropertyOptionExpression;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;

public abstract class AbstractPropertiesLoadable implements ApplicationContextAware, EmbeddedValueResolverAware{

	protected final Log logger = LogFactory.getLog(getClass());
	
	protected StringValueResolver resolver;
	protected ApplicationContext applicationContext;

	/** 配置文件路径 */
	protected String propertyPath;
	
	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		this.resolver = resolver;
	}

	public final void setApplicationContext(ApplicationContext context) throws BeansException {
		applicationContext = context;
	}
	
	public void setPropertyPath(String propertyPath) {
		this.propertyPath = propertyPath;
	}

	/**
	 * 把配置文件的定义内容读取成一个数组
	 * @param path
	 * @return
	 * @throws Exception
	 */
	protected PropertiesResource readLines(String path) throws Exception{
		return PropertyOptionExpression.optionConfig(new PropertiesResource(applicationContext, resolver)).load(path);
	}

}
